<template>
  <div class="autotrophy">
    <div class="common">
      <div class="common-input">
        <el-input style="width: 300px" v-model="commonValue" placeholder="订单号、昵称、手机号" clearable></el-input>
      </div>

      <div class="common-input">
        <el-select v-model="commonOrderValue" placeholder="请选择订单类型" clearable>
          <el-option v-for="item in commonOrderOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </div>

      <div class="common-input">
        <el-select v-model="commonStateValue" placeholder="请选择订单状态" clearable>
          <el-option v-for="item in commonStateOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </div>

      <div class="common-input">
        <el-select v-model="commonShopValue" placeholder="请选择时间类型" clearable>
          <el-option v-for="item in commonShopOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </div>

      <div class="common-input">
        <el-date-picker v-model="commonDate" type="daterange" range-separator="至" start-placeholder="开始日期"
          end-placeholder="结束日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd">
        </el-date-picker>
      </div>

      <div class="common-button" @click="searchHit">搜索</div>
      <div class="common-button" @click="replacementHit">重置</div>
    </div>

    <div class="table" v-loading="loading">
      <el-table ref="multipleTable" tooltip-effect="dark" @selection-change="handleSelectionChange" :data="tableData"
        style="width: 100%" border :header-cell-style="{
          color: '#333333',
          backgroundColor: '#f3f3f3',
        }">
        <el-table-column type="selection" width="70" align="center">
        </el-table-column>
        <el-table-column prop="orderNo" label="订单号"></el-table-column>
        <el-table-column prop="" label="下单用户信息">
          <template slot-scope="scope">
            <div class="title">{{ scope.row.nickname }}</div>
            <div class="title">{{ scope.row.phone }}</div>
            <div class="title">{{ scope.row.userVipName }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="paymentPrice" label="实际付款（元）" width="180">
        </el-table-column>
        <el-table-column prop="" label="订单状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.orderStatus == 'WAIT'">待付款</el-tag>
            <el-tag v-else-if="scope.row.orderStatus == 'PAYMENT'">已付款</el-tag>
            <el-tag v-else-if="scope.row.orderStatus == 'CANCEL'">已取消</el-tag>
            <el-tag v-else-if="scope.row.orderStatus == 'SEND'">待发货</el-tag>
            <el-tag v-else-if="scope.row.orderStatus == 'START'">已发货</el-tag>
            <el-tag v-else-if="scope.row.orderStatus == 'SIGN'">待签收</el-tag>
            <el-tag v-else-if="scope.row.orderStatus == 'FINISH'">已签收</el-tag>
            <el-tag v-else-if="scope.row.orderStatus == 'REJECTION'">已拒收</el-tag>
            <el-tag v-else-if="scope.row.orderStatus == 'AFTER'">售后中</el-tag>
            <el-tag v-else-if="scope.row.orderStatus == 'RETURN'">售后完成</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="" label="创建时间" width="300">
          <template slot-scope="scope">
            <div class="title">创建时间：{{ scope.row.createTime }}</div>
            <div class="title">支付时间：{{ scope.row.payTime }}</div>
            <div class="title">发货时间：{{ scope.row.sendTime }}</div>
            <div class="title">签收时间：{{ scope.row.finishTime }}</div>
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="300">
          <template slot-scope="scope">
            <div class="operation">
              <div class="list" @click="deliverHit(scope.row)" v-if="scope.row.orderStatus == 'SEND'">发货</div>
              <div class="list" @click="detailsHit(scope.row)">查看详情</div>
              <div class="list" @click="logisticsHit(scope.row)"
                v-if="scope.row.orderStatus != 'WAIT' && scope.row.orderStatus != 'SEND' && scope.row.orderStatus != 'CANCEL' ">
                查看物流</div>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
          layout="prev, pager, next" :total="tableDataTotal" :current-page="page">
        </el-pagination>
      </div>
    </div>
    <!--新增或者编辑-->
    <div class="add">
      <el-dialog title="发货" :visible.sync="addVisible" :before-close="addClose" width="25%">
        <div class="add-form">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
            <el-form-item label="快递公司" prop="express">
              <el-select v-model="ruleForm.express" placeholder="请选择快递公司" style="width: 100%" @change="expressChange">
                <el-option v-for="(item, index) in logisticsList" :key="index" :label="item.logisticsName"
                  :value="item.logisticsId"> </el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="物流单号" prop="order">
              <el-input v-model="ruleForm.order" placeholder="请输入物流单号" clearable></el-input>
            </el-form-item>

            <el-form-item class="attachment">
              <div class="list" @click="addClose">取消</div>
              <div class="list" @click="addConfirm('ruleForm')">保存</div>
            </el-form-item>
          </el-form>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  orderList,
  orderLogisticsList,
  orderShipmentsList
} from '@/api/order'


export default {
  data() {
    return {
      commonValue: "",
      commonStateValue: "",
      commonStateOptions: [
        {
          value: "-1",
          label: "全部",
        },
        {
          value: "WAIT",
          label: "待付款",
        },
        {
          value: "PAYMENT",
          label: "已付款",
        },
        {
          value: "SEND",
          label: "待发货",
        },
        {
          value: "START",
          label: "已发货",
        },
        {
          value: "SIGN",
          label: "待签收",
        },
        {
          value: "FINISH",
          label: "已签收",
        },
      ],
      commonShopValue: "",
      commonShopOptions: [
        {
          value: "0",
          label: "创建时间",
        },
        {
          value: "1",
          label: "支付时间",
        },
        {
          value: "2",
          label: "发货时间",
        },
        {
          value: "3",
          label: "签收时间",
        },
      ],

      commonOrderValue: '',
      commonOrderOptions: [
        {
          value: "0",
          label: "正常订单",
        },
        {
          value: "1",
          label: "盲盒订单",
        },
        {
          value: "2",
          label: "领福利",
        },
      ],

      commonDate: [],
      commonStart: "",
      commonEnd: "",
      //-------------
      multipleSelection: [],
      page: 1,
      limit: 10,
      tableData: [],
      tableDataTotal: 0,
      addRoaid: 0,
      loading: false,
      commonObj: {},
      //--------
      addVisible: false,
      logisticsList: [],
      ruleForm: {
        order: "",
        express: "",
        name: ''
      },
      rules: {
        order: [{ required: true, message: "请输入物流单号", trigger: "blur" }],
        express: [
          { required: true, message: "请选择快递公司", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.orderListMet()
  },
  methods: {
    orderListMet() {
      let params = {
        orderType: this.commonOrderValue,
        orderStatus: this.commonStateValue == -1 ? '' : this.commonStateValue,
        keyWord: this.commonValue,
        timeType: this.commonShopValue,
        startTime: this.commonStart,
        endTime: this.commonEnd,

        pageSize: this.limit,
        pageNo: this.page,
      }

      orderList(params).then(res => {
        if (res.errorCode == null) {
          res.resultData.orderList.forEach((item, index) => {
            item.paymentPrice = Number(item.paymentPrice / 100).toFixed(2);
            item.retailPrice = Number(item.retailPrice / 100).toFixed(2);
            item.price = Number(item.price / 100).toFixed(2);
            item.realityFreight = Number(item.realityFreight / 100).toFixed(2);

          });

          this.tableData = res.resultData.orderList;
          this.tableDataTotal = res.resultData.totalNum;
          this.loading = false;
        }
      })
    },
    //-------------
    timeData(date) {
      return date.toLocaleString("zh-CN", {
        year: "numeric",
        month: "2-digit",
        day: "2-digit",
        // hour: "2-digit",
        // minute: "2-digit",
        // second: "2-digit",
      });
    },
    searchHit() {
      if (this.commonDate.length > 0) {
        this.commonStart = this.timeData(new Date(this.commonDate[0]));
        this.commonEnd = this.timeData(new Date(this.commonDate[1]));
      } else {
        this.commonStart = "";
        this.commonEnd = "";
      }

      this.page = 1;
      this.loading = true;
      this.orderListMet();
    },
    replacementHit() {
      this.commonDate = [];
      this.commonStart = "";
      this.commonEnd = "";
      this.commonValue = "";
      this.commonStateValue = "";
      this.commonShopValue = ''
      this.commonOrderValue = ''

      this.page = 1;
      this.loading = true;
      this.orderListMet();
    },

    //------------
    headerHit(index) {
      this.headerIndex = index;
    },
    deliverHit(item) {
      if (this.$store.state.premissions.infoRouterShow == false) {
        this.$message.error("暂无权限，请联系管理员开通");
        return;
      }

      let params = {}

      orderLogisticsList(params).then(res => {
        if (res.errorCode == null) {
          this.commonObj = item
          this.logisticsList = res.resultData
          this.addVisible = true;
        }
      })

    },
    expressChange(e) {
      this.ruleForm.express = e

      this.logisticsList.forEach((item, index) => {
        if (e == item.logisticsId) {
          this.ruleForm.name = item.logisticsName
        }
      })

    },

    addClose() {
      this.commonObj = {};
      this.ruleForm.order = "";
      this.ruleForm.express = "";
      this.addVisible = false;
    },
    addConfirm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {

          let params = {
            orderId: this.commonObj.orderId,
            logisticsId: this.ruleForm.express,
            logisticsName: this.ruleForm.name,
            logisticsNo: this.ruleForm.order
          }


          orderShipmentsList(params).then(res => {
            if (res.errorCode == null) {
              this.$message.success('发货成功！')
              this.addVisible = false;
              this.commonObj = {};
              this.ruleForm.order = "";
              this.ruleForm.express = "";
              this.ruleForm.name = "";
              this.page = 1;
              this.loading = true;
              this.orderListMet();
            }
          })

        } else {
          return false;
        }
      });
    },
    //-----------
    detailsHit(item) {
      if (this.$store.state.premissions.infoRouterShow == false) {
        this.$message.error("暂无权限，请联系管理员开通");
        return;
      }
      this.$router.push({
        path: "/order/merchandise/merchandise-self/merchandise-self-support-details",
        query: {
          id: item.orderId
        }
      });
    },
    logisticsHit(item) {
      console.log('2222', item)
      if (this.$store.state.premissions.infoRouterShow == false) {
        this.$message.error("暂无权限，请联系管理员开通");
        return;
      }
      this.$router.push({
        path: "/order/merchandise/merchandise-self/merchandise-self-support-logistics",
        query: {
          logisticsId: item.logisticsId,
          logisticsNo: item.logisticsNo
        }
      });
    },
    //--------------------------------------
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //---------
    handleSizeChange(val) {
      this.limit = val;
      this.loading = true;
      this.orderListMet();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.loading = true;
      this.orderListMet();
    },
  },
};
</script>

<style lang="less" scoped>
@import url("@/assets/css/official.less");
@import url("@/assets/css/common.less");

.autotrophy {
  .common {
    margin-bottom: 10px;
    display: flex;
    align-items: center;

    .common-input {
      margin-right: 10px;

      /deep/ .el-input {
        width: 250px;
      }
    }

    .common-input:last-child {
      margin-right: 0;
    }

    .common-button {
      padding: 9px 20px;
      box-sizing: border-box;
      border-radius: 5px;
      background: #2a82e4;
      font-size: 14px;
      color: #fff;
      margin-left: 20px;
      cursor: pointer;
    }

    .common-button:last-child {
      background: #999999;
    }

    .common-button:first-child {
      margin-left: 0;
    }

    .header {
      width: 100%;
      display: flex;
      align-items: center;

      .list {
        width: 100px;
        height: 35px;
        background: #999999;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #fff;
        cursor: pointer;
        margin-right: 10px;
        border-radius: 4px;
      }

      .active {
        background: #2a82e4;
      }
    }
  }

  .table {
    .title {
      margin-bottom: 10px;
    }
  }

  .add {
    /deep/ .el-dialog__header {
      font-size: 16px;
      text-align: center;
    }

    /deep/ .el-dialog__body {
      padding-bottom: 10px;
    }

    .add-form {
      /deep/ .el-select {
        width: 100%;
      }

      .img {
        width: 70px;
        height: 70px;
        display: flex;
        align-items: center;
        justify-content: center;
        border: 1px solid #ccc;
        border-radius: 5px;

        img {
          width: 100%;
          height: 100%;
          border-radius: 5px;
        }
      }

      .attachment {
        margin-top: 50px;

        /deep/ .el-form-item {
          margin-bottom: 0;
        }

        /deep/ .el-form-item__content {
          margin-left: 0 !important;
          display: flex;
          align-items: center;
          justify-content: center;
        }

        .list {
          background: #3da8f5;
          color: #fff;
          font-size: 16px;
          margin-right: 50px;
          border-radius: 5px;
          width: 100px;
          height: 35px;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
        }

        .list:first-child {
          background: #808080;
        }

        .list:last-child {
          margin-right: 0;
        }
      }
    }
  }
}
</style>